<?php
include_once(JL_ROOT.'/jl_controller.cls.php');
include_once(JL_ROOT.'/jl_tools.cls.php');

class SameDo extends JL_controller {
	
	public function __init(){
		$this->page = JL_factory::get_JL_page();
		
		
	}
	
	public function do__(){
		
		$this->do__output();	
		
		
		
		
		
	}


	public function do__output(){
		
		$table = JL_factory::get_JL_mysql_table('jl_same_output');
		$table->get_rows('*'," ORDER BY same_output_time DESC LIMIT 1000");
		$res = $table->exec();
		$output_list;
		$me = JL_tools::get_session('me');
		$me_tag = $me['tag'];
		$me_tag_arr = explode(',',$me_tag);
		while($row = mysql_fetch_assoc($res)){
			$row_tag_arr = explode(',',$row['same_output_tag']);
			if(count(array_intersect($me_tag_arr,$row_tag_arr))>0){//如果本条记录TAG与我的TAG有交集
				$output_list[]=$row;
			}

		}
		$this->page->set_item('me_tag',$me_tag);
		$this->page->set_item('me_tag_arr',$me_tag_arr);
		$this->page->set_item('output_list',$output_list);
		$this->page->display('/same/output_list.tpl.php');
		

	}

	public function do__echo(){

		$op_content = $_REQUEST['post_output'];
		$me = JL_tools::get_session('me');
		$me_tag = $me['tag'];
		$time = time();
		$table = JL_factory::get_JL_mysql_table('jl_same_output');
		$a = array('same_output_content'=>$op_content,
				'same_output_time'=>$time,
				'same_output_datetime'=>date('Y-m-d H:i:s',$time),
				'same_output_tag'=>$me_tag,
				'same_output_user_id'=>$me['user_id'],
				'same_output_user_name'=>$me['user_name'],
				'same_output_user_pic'=>'http://www.jllee.com/ucenter/data/avatar/000/00/00/02_avatar_big.jpg',
				);
		$table->new_rows($a);
		$table->exec();
		header('location: /same.php');exit;


	}
	public function do__set_tag(){

		$tag = $_REQUEST['tag'];
		$table = JL_factory::get_JL_mysql_table('jl_user_tag');
		$me = JL_tools::get_session('me');
		$table->set_rows(array('user_tag_content'=>$tag)," user_tag_uid={$me['user_id']}");
		$me['tag']=$tag;
		JL_tools::set_session('me',$me);
		header('location:/same.php');

	}
/*
	public function do__reg(){
	
		$login_name = $_REQUEST['login_name'];
		$login_password = $_REQUEST['login_password'];
		$name = $_REQUEST['user_name'];
		
		$user_a = array('user_login_name'        =>    $login_name,
				'user_login_passwords'   =>    md5($login_password),
				'user_name'              =>    $name,
				'user_reg_time'          =>    time(),
				'user_reg_ip'            =>    'ip',
				'user_email'             =>    $email,
				);
		
		if(empty($this->func_get_user($login_name))){
			$this->func_reg($user_a);
			//注册成功后
		}else{
			$this->page->set_error('error_title','您的注册存在错误');
			$this->page->set_error('error','登陆名已经存在');
			$this->page->diaplsy('/user/errors.tpl.php');exit;
		}
		
	}

	public function do__login(){
		$login_name = $_REQUEST['login_name'];
		$login_password = $_REQUEST['login_password'];
		$user = $this->func_get_user($login_name);
		if($user['user_login_password']==md5($login_password)){//如果密码正确
			JL_tools::set_session('me',$user);

			//登陆成功后，修改用户最近登陆时间IP以及其他资料
		}else{
			$this->page->set_error('error_title','您的登陆存在错误');
			$this->page->set_error('error','用户名或密码不正确');
		}

	}

	///*用户注册
	public function func_reg($user_data){

		$table = JL_factory::get_JL_mysql_table('users');
		$table -> new_rows($user_data);
		$table -> exec();
		return true;

	}
	
	//*查询用户
	public function func_get_user($user_id_or_loginname){

		$table = JL_factory::get_JL_mysql_table('users');
		$user_id_or_loginname = mysql_escape_string($user_id_or_loginname);
		$sql = "SELECT * FROM users WHERE user_id='{$user_id_or_loginname}' UNION SELECT * FROM users WHERE user_login_name='{$user_id_or_loginname}'";
		$table -> query($sql);
		return mysql_fetch_assoc($table -> exec());


	}
	
	//*根据条件查询用户
	public function func_get_user_where($where){

		$table = JL_factory::get_JL_mysql_table('users');
		$user_id_or_loginname = mysql_escape_string($user_id_or_loginname);
		$sql = "SELECT * FROM users WHERE {$where}";
		$table -> query($sql);
		return mysql_fetch_assoc($table -> exec());


		

	}


	
	
	
	public function do__user_reg(){
		$uname = trim($_REQUEST['u']);
		$pwd = $_REQUEST['p'];
		$pwd2 = $_REQUEST['p2'];
		$email = $_REQUEST['email'];
		$errors = null;
		
		
		if(strlen(trim($uname))<6){
			$errors .= '用户名不能少于6个字符<br />';
		}
		if($this->func_get_user_info(trim($uname))){
			$errors .= '用户名已经存在<br />';
		}
		if(strlen($pwd)<6){
			$errors .= '密码不能少于6个字符<br />';
		}
		if($pwd!==$pwd2){
			$errors .= '两次密码输入不一致<br />';
			
		}
		if(strlen($email)<1){
			$errors .= 'email不能为空<br />';
		}
		if(!empty($errors)){
			$this->page->set_error('error',$errors);
			$this->page->display('/user/error.tpl.php');exit;
		}
		
		
		$a = array(
						'user_login_name'        => $uname,
						'user_login_password'    => md5($pwd),
						'user_login_email'       => $email,
							);
		
		$table = JL_factory::get_JL_mysql_table('zb_user');
		$table->new_rows($a);
		
		$table->exec();
		
		$this->page->set_item('message','注册成功');
		$this->page->set_item('go_url','/');
		$this->page->display('/user/message.tpl.php');exit;
		
		
		
		
		
	}
	
	
	public function do__login(){
		$uname = $_REQUEST['u'];
		$pwd = $_REQUEST['p'];
		$url = $_REQUEST['url'];
		if($this->func_login($uname,$pwd)){
			
			
			if(!empty($url)){
				header('location:'.$url);exit;
			}else{
				echo '显示消息:登陆成功';
				
			}
			
		}else{
			echo '提示错误:密码错误';
		}
		
	}
	
	//登陆方法
	public function func_login($uname,$pwd){
		$u_info = $this->func_get_user_info($uname);
		
		if($u_info['user_login_password']==md5($pwd)){
			JL_tools::set_session('login_user',$u_info);

			return true;
		}else{
			
			return false;
		}
	}
	
	//得到用户信息
	public function func_get_user_info($uname){
		
		$table = JL_factory::get_JL_mysql_table('zb_user');
		$table->get_rows('*',"WHERE user_login_name='{$uname}'");
		
		return mysql_fetch_assoc($table->exec());
		
		

		
	}
	
	*/
	
	
}















?>
